﻿Imports System.Resources
Imports System.Xml
Imports Aricie.Collections

Namespace Configuration.Templating.Resource
    Public Class ResxResourceHandler
        Implements IInternalResourceHandler
        Implements ISerializableResourceHandler

        Private _path As String
        Private _KeyValues As New SerializableDictionary(Of String, String)
        Private _IHaveBeenChanged As Boolean = False



        Public Sub New(resourcefile As String)
            _path = resourcefile

            Try
                If (System.IO.File.Exists(_path)) Then

                    ' on force le fichier a être lisible
                    System.IO.File.SetAttributes(_path, IO.FileAttributes.Normal)

                    Using fs As New System.IO.FileStream(_path, IO.FileMode.OpenOrCreate)
                        Using rr As New ResourceReader(fs)
                            Dim dict As IDictionaryEnumerator = rr.GetEnumerator()
                            While dict.MoveNext()
                                If (dict.Key IsNot Nothing AndAlso dict.Value IsNot Nothing) Then
                                    _KeyValues.Add(dict.Key.ToString(), dict.Value.ToString())
                                End If
                            End While
                        End Using
                    End Using
                Else
                    _IHaveBeenChanged = True
                    CommitChanges()
                End If
            Catch ex As Exception
                Aricie.Services.ExceptionHelper.LogException(ex)
            End Try
        End Sub

        Public ReadOnly Property Filepath As String Implements IInternalResourceHandler.Filepath
            Get
                Return _path
            End Get
        End Property

        Public Sub CommitChanges() Implements IInternalResourceHandler.CommitChanges
            If _IHaveBeenChanged Then

                Using fs As New System.IO.FileStream(_path, IO.FileMode.OpenOrCreate)
                    Using rw As New ResourceWriter(fs)
                        For Each entry In _KeyValues
                            rw.AddResource(entry.Key, entry.Value)
                        Next
                    End Using
                End Using

                _IHaveBeenChanged = False
            End If
        End Sub


        Default Public Property Item(key As String) As String Implements IInternalResourceHandler.Item
            Get
                If (_KeyValues.ContainsKey(key)) Then
                    Return _KeyValues(key)
                End If
                Return Nothing
            End Get
            Set(value As String)
                If (_KeyValues.ContainsKey(key)) Then
                    _KeyValues(key) = value
                Else
                    _KeyValues.Add(key, value)
                End If
                _IHaveBeenChanged = True
            End Set
        End Property

        Public Sub DeleteKey(key As String) Implements IInternalResourceHandler.DeleteKey
            If (_KeyValues.ContainsKey(key)) Then
                _KeyValues.Remove(key)
            End If
        End Sub

        Public Function AsSerializable() As Aricie.Collections.SerializableDictionary(Of String, String) Implements ISerializableResourceHandler.AsSerializable
            Return _KeyValues
        End Function

        Public Sub FromSerializable(IXS As Aricie.Collections.SerializableDictionary(Of String, String)) Implements ISerializableResourceHandler.FromSerializable
            _KeyValues = IXS
            _IHaveBeenChanged = True
            CommitChanges()
        End Sub
    End Class
End Namespace